//
//  XGL_PersonalPickerView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/20.
//

import UIKit
import CollectionKit

class XGL_PersonalPickerView: UIView {
    
    typealias SelectItemAction = (_ selectIndex: NSInteger)->Void
    private var contentView: UIView!
    private var pickerView: UIView!
    private var collectionView: CollectionView!
    
    var selectHandler: SelectItemAction?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.createXglView()
        self.setCollectionData()
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissAction))
        tap.delegate = self
        self.addGestureRecognizer(tap)
        
        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.25) {
            self.contentView.transform = CGAffineTransform(translationX: 0, y: -self.contentView.bounds.height)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layoutIfNeeded()
        self.pickerView.addCorner(conrners: [.topLeft,.topRight], radius: 24.0)
    }
}

extension XGL_PersonalPickerView{
    @objc private func pickItemSelectAction(sender: UIButton){
        let index = sender.tag - 2000
        if let block = self.selectHandler {
            block(index)
        }
        self.dismiss()
    }
    @objc private func dismissAction(){
        self.dismiss()
    }
}

extension XGL_PersonalPickerView{
    private func dismiss(){
        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.25) {
            self.contentView.transform = .identity
        } completion: { _ in
            self.removeFromSuperview()
        }

    }
}

extension XGL_PersonalPickerView: UIGestureRecognizerDelegate{
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let touchPoint = touch.location(in: self)
        self.contentView.layoutIfNeeded()
        if self.contentView.frame.contains(touchPoint){
            return false
        }
        
        return true
    }
}

extension XGL_PersonalPickerView{
    private func createXglView(){
        self.contentView = UIView()
        let topIconView = UIImageView(image: imageWithName("icon_xgl_personal_picker"))
        pickerView = UIView().then { make in
            make.backgroundColor = UIColor.hexColor("#001310")
        }
        collectionView = CollectionView()
        self.contentView.addSubviews([pickerView,topIconView,self.collectionView])
        self.addSubview(self.contentView)
        topIconView.snp.makeConstraints { make in
            make.centerX.top.equalToSuperview()
            make.width.equalTo(79.0)
            make.height.equalTo(69.0)
        }
        pickerView.snp.makeConstraints { make in
            make.top.equalTo(topIconView.snp.centerY)
            make.centerX.width.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        self.contentView.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.height.equalTo(conversionWithScrennWidth(value: 500.0))
            make.top.equalTo(self.snp.bottom)
        }
        collectionView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(62.0)
        }
    }
    
    private func setCollectionData(){
        let datas: [String] = ["Edit Profile","Block List","Terms of Use","Privacy Policy","Version \(AppversionCodeStr)","Delete Account","Log Out"]
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: String, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let titleText = UILabel().then { make in
                make.text = data
                make.textColor = UIColor.white
                make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            }
            let moreIconView = UIImageView(image: imageWithName("icon_more_right"))
            let button = UIButton().then { make in
                make.tag = index + 2000
                make.addTarget(self, action: #selector(self.pickItemSelectAction(sender:)), for: .touchUpInside)
            }
            view.addSubviews([titleText,moreIconView,button])
            titleText.snp.makeConstraints { make in
                make.left.equalTo(15.0)
                make.centerY.equalToSuperview()
            }
            moreIconView.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.right.equalTo(-15.0)
//                make.width.height.equalTo(18.0)
            }
            button.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        })
        let sizeSource = { (index: Int, data: String, collectionSize: CGSize) -> CGSize in
            return CGSize(width: collectionSize.width, height: 60.0)
        }
        let provider = BasicProvider(
            dataSource: datas,
            viewSource: viewSource,
            sizeSource: sizeSource
        )
        self.collectionView.provider = provider
    }

}
